package com.atguigu1.core.serial

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable

/**
 *
 * @description: 行动算子案例
 * @time: 2021-03-12 11:45
 * @author: baojinlong
 **/
object Spark01SerialDemo {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("rdd")
    // 设置rdd分区数字
    val sparkContext = new SparkContext(conf)
    val inputRddValue: RDD[String] = sparkContext.makeRDD(Seq("Hello ajax", "Hello Scala", "hive", "atguigu", "Hello Spark", "Hello windy", "windy alice"), numSlices = 2)
    val search = new Search("h")
    // search.getMatch1(inputRddValue).collect.foreach(println)
    search.getMatch2(inputRddValue).collect.foreach(println)
    sparkContext.stop()
  }
}

// 类的构造参数其实就是类的数星星,构造参数需要进行闭包检测,其实就等同于类进行闭包检测,extends,case或者42行代码都可以解决
class Search(query: String) {
  def isMatch(s: String): Boolean = {
    s.contains(this.query)
  }

  def getMatch1(rdd: RDD[String]): RDD[String] = {
    rdd.filter(isMatch)
  }

  def getMatch2(rdd: RDD[String]): RDD[String] = {
    // 也可以通过下面方法解决序列化
    val inputQuery: String = query
    rdd.filter(x => x.contains(inputQuery))
  }
}